package com.yh.zz_hlj.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import com.yh.zz_hlj.AppContext;
import com.yh.zz_hlj.api.RetrofitSingleton;
import com.yh.zz_hlj.base.BaseListFragment;
import com.yh.zz_hlj.common.StringUtils;
import com.yh.zz_hlj.modules.adatper.CustPhoneAdapter;
import com.yh.zz_hlj.modules.adatper.ListBaseAdapter;
import com.yh.zz_hlj.modules.bean.CustPhone;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * =========================
 * 版权所有，违法必究
 * 作者：john
 * <p>
 * 工程：FoodBarcode
 * <p>
 * 文件名：CustPhoneListFragment
 * <p>
 * 创建时间：2017.05.23.9:32
 * *****************************
 */
public class CustPhoneListFragment extends BaseListFragment<CustPhone> {


    private String mType;
    private String mold;
    private String name = "all";


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        mType = bundle.getString("type");
        mold = bundle.getString("mold");
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        img_shaixuan.setVisibility(View.GONE);
    }

    @Override
    protected ListBaseAdapter getListAdapter() {
        return new CustPhoneAdapter(mType);
    }

    @Override
    protected void sendRequestData(String id) {
        super.sendRequestData(id);
        showWaitDialog();
        if ("gys".equals(mType)) {
            RetrofitSingleton.getApiService(mActivity).
                    SupplyAll(name, mCurrentPage, AppContext.PAGE_SIZE)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(observer);
        } else if ("xm".equals(mType)) {
            RetrofitSingleton.getApiService(mActivity).
                    ProjectAll(name, mCurrentPage, AppContext.PAGE_SIZE)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(observer);
        } else if ("ck".equals(mType)) {
            RetrofitSingleton.getApiService(mActivity).
                    StockAll(name, mCurrentPage, AppContext.PAGE_SIZE)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(observer);
        } else if ("bm".equals(mType)) {//部门
            RetrofitSingleton.getApiService(mActivity).
                    DeptAll(name, mCurrentPage, AppContext.PAGE_SIZE)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(observer);
        }else if ("cklx".equals(mType)) {
            RetrofitSingleton.getApiService(mActivity).
                    SaleTypeAll(name, mCurrentPage, AppContext.PAGE_SIZE)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(observer);
        }
        name = "all";
    }

    @Override
    public void inputAction(String str) {
        super.inputAction(str);
        mCurrentPage = 1;
        mAdapter.clear();
        if (!StringUtils.isEmpty(str)) {
            name = str;
        } else {
            name = "all";
        }
        sendRequestData(null);
    }

    @Override
    public void searchAction(int position) {
        super.searchAction(position);
    }

    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        super.onItemClick(parent, view, position, id);
        CustPhone item = mAdapter.getItem(position);
        if (item != null) {
            if ("com".equals(mold)) {
                if (listener != null) {
                    listener.onComClick(item);
                    mActivity.finish();
                }
                return;
            }
            if ("gift".equals(mold)) {
                if (listener != null) {
                    listener.onGiftClick(item);
                    mActivity.finish();
                }
                return;
            }
            Intent intent = new Intent();
            intent.putExtra("item", item);
            mActivity.setResult(0, intent);
            mActivity.finish();
        }
    }

    public static void setOnItemClickListener(OnItemClickListener listener) {
        CustPhoneListFragment.listener = listener;
    }

    public static OnItemClickListener listener;

    public interface OnItemClickListener {
        void onComClick(CustPhone item);

        void onGiftClick(CustPhone item);
    }
}
